package controller;

import model.MySql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户注册;
 * Created by yvdedu.com on 2016/9/26.
 */
@WebServlet(urlPatterns = "/register_user")
public class register_user extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        if (name != null && pwd != null) {
            MySql mySql = new MySql();
            model.User user = mySql.getUserByName(name);
            if (user == null) {
                Boolean result = mySql.addUser(name, pwd);
                if (result) {
                    resp.getWriter().print("恭喜你，注册成功！");
                } else {
                    resp.getWriter().print("对不起，注册失败，请重试！");
                }
            } else {
                resp.getWriter().print("用户名已被注册，请更换重试！");
            }
        } else {
            resp.getWriter().print("用户名或密码不能为空！");
        }
    }
}
